// pages/activity/pre_sell_list/pre_sell_list.js
var app = getApp();
var util = require('../../../utils/util.js');
var request = app.request;
var setting = app.globalData.setting;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    shop_list: [],
    pages: 1,
    no_loding: true,
    yes_lam: false,
    url: setting.url,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '预售活动'
    })
  },

  // 获取商品数据
  shop_list: function () {
    var that = this;
    var data = {
      type: that.data.activeCategoryId,
      p: that.data.pages,
      listRows: 10,
    }
    request.get('/api/Activity/pre_sell_list', {
      data: data,
      failRollback: true,
      successReload: true,
      success: function (res) {
        if (res.data.status == 1) {
          if (res.data.result.groups.length > 0) {
            res.data.result.groups.forEach(function (ele, index) {
              var url = ele.img;
              if (url && !url.indexOf('http') == 0) {
                res.data.result.groups[index].img = setting.url + url;
              }
            })
          }
          if (res.data.result.groups.length < 10) {
            that.setData({ no_loding: false })
          }
          var shop = res.data.result.groups;
          var pages = parseInt(that.data.pages) + 1;
          for (var i = 0; i < shop.length; i++) {
            that.data.shop_list.push(shop[i])
          }
          that.setData({ shop_list: that.data.shop_list, pages: pages })
        }
        clearInterval(that.data.timer)
        if (that.data.shop_list.length > 0) {
          that.createTimer(that.data.shop_list)
        }
      }
    });
  },

  createTimer: function (lmes) {
    var that = this;
    this.data.timer = setInterval(() => {
      var startTime = (new Date()).getTime();
      var remainTime = 0;
      var list = lmes;
      for (var i = 0; i < list.length; i++) {
        let diffTime = (list[i].sell_end_time * 1000 - startTime);
        if (diffTime > 0) {
          var data = Math.floor(diffTime / (1000 * 60 * 60 * 24));
          var hours = Math.floor((diffTime / (1000 * 60 * 60)) % 24);
          var time = Math.floor((diffTime / (1000 * 60)) % 60);
          var secondms = Math.floor((diffTime / 1000) % 60);
          if (hours < 10) { var hours = '0' + hours; }
          if (time < 10) { var time = '0' + time; }
          if (secondms < 10) { var secondms = '0' + secondms; }
          var diffTime_add = hours + '时' + time + "分" + secondms + '秒';
          list[i].diffTime_add = diffTime_add;
        }
      }
      that.setData({ shop_list: list });
    }, 1000);
  },


  da_img:function(e){
    var goods = e.currentTarget.dataset.goods;
    if (this.data.yes_lam){
      return false;
    }
    this.setData({yes_lam: false })
    wx.navigateTo({
      url: `/pages/goods/goodsInfo/goodsInfo?goods_id=${goods}`,
    })
  },
  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({ pages: 1, shop_list: [], no_loding: true, yes_lam: false })
    this.shop_list();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(this.data.timer);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.no_loding) {
      this.shop_list();
    } else {
      app.showWarning('加载完啦', null, 500, false);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})